using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace TidhiGanitam.DataModel
{
    public class KaranamuRoutineItem
    {
        double m_KaranamTidhi;
        KaranamuItem m_KaranamuItem;
        TidhiRoutineItem m_TidhiItem;
        public KaranamuItem KaranamuItem
        {
            get
            { return m_KaranamuItem; }
            set
            { m_KaranamuItem = value; }
        }

        public KaranamuRoutineItem(TidhiRoutineItem CurrentTidhiRoutine, double KaranamTidhi)
        {
            m_KaranamuItem = new KaranamuItem();
            m_TidhiItem = CurrentTidhiRoutine;
            m_KaranamTidhi = KaranamTidhi;
        }
        public void Calculate(DinamuRoutineItem PreviousDinamuItem, TidhiRoutineItem PreviousTidhiRoutine, KaranamuRoutineItem PreviousKaranamuRoutine)
        {
            if (PreviousTidhiRoutine.PreviousTidhiState != Enums.TidhiState.Purti)
            {
                Rasi PreviousDinaPramanam = new Rasi(PreviousDinamuItem.RojuPramanam);
                //Calculating Karanamu
                Rasi PreviousTidhiTimes = new Rasi();
                foreach (TidhiDetails tidhi in PreviousTidhiRoutine.TidhuluDetails)
                {
                    PreviousTidhiTimes += tidhi.EndTime;
                }
                Rasi TidhiTime = new Rasi();
                TidhiTime = new Rasi(((TidhiDetails)m_TidhiItem.TidhuluDetails[0]).EndTime);
                Rasi ReminingCurrentTidhi = PreviousDinaPramanam - PreviousTidhiTimes;
                //if(m_TidhiItem.TidhuluDetails
                Rasi TotalTidhiTime = ReminingCurrentTidhi + TidhiTime;
                if (m_TidhiItem.PreviousTidhiState == Enums.TidhiState.Purti)
                {
                    //We have 1st karanamu
                    m_KaranamuItem.Karanamu = Constants.Karanam[Convert.ToInt32(m_KaranamTidhi), 0];
                    m_KaranamuItem.KaranamuTime = new Rasi(TotalTidhiTime);
                    return;
                }

                Rasi HalfTidhiTime = TotalTidhiTime / 2;

                Rasi SuryodayamSpan = Rasi.Difference(HalfTidhiTime , ReminingCurrentTidhi);
                Rasi KaranamTime = HalfTidhiTime;
                if (SuryodayamSpan > new Rasi(0, 0, 0, 0))
                {
                    //We have 1st karanamu
                    m_KaranamuItem.Karanamu = Constants.Karanam[Convert.ToInt32(m_KaranamTidhi), 0];
                    m_KaranamuItem.KaranamuTime = SuryodayamSpan;
                }
                else
                {
                    //We have 2nd karanamu
                    m_KaranamuItem.Karanamu = Constants.Karanam[Convert.ToInt32(m_KaranamTidhi), 1];
                    m_KaranamuItem.KaranamuTime = new Rasi(TidhiTime);
                }
            }
            else
            {
                //We have 2nd karanamu
                m_KaranamuItem.Karanamu = Constants.Karanam[Convert.ToInt32(m_KaranamTidhi), 1];
                m_KaranamuItem.KaranamuTime = new Rasi(m_TidhiItem.TidhiTime);

                Rasi TotalTidhiTime = PreviousKaranamuRoutine.m_KaranamuItem.KaranamuTime //this is last 2 days total
                                        + m_TidhiItem.TidhiTime ; //this is todays value
                //we need to set the karanamu for previous day also
                Rasi TwoDaysBackValue = PreviousKaranamuRoutine.m_KaranamuItem.KaranamuTime - PreviousDinamuItem.RojuPramanam;
                Rasi HalfTidhiTime = TotalTidhiTime / 2;
                Rasi KaranmuTime = HalfTidhiTime - TwoDaysBackValue;
                PreviousKaranamuRoutine.m_KaranamuItem.KaranamuTime = new Rasi(KaranmuTime);
            }
        }
    }
}
